// Herson

#include "nim.h"
#include <WinSock2.h>
#include <iostream>

// getServers.cpp
//  Given a socket, a broadcast address and a port number, this function returns the number
//  of servers as well as an array of structs.  Each struct contains the name, IP_Address and 
//  port number of a remote server.
#define SECs_TO_WAIT 5
#define MIL_TO_WAIT 0

void getServers(SOCKET s, char *broadcastAddress, char *broadcastPort, ServerStruct server[], int &numServers)
{
//	IN parameters:
//		SOCKET s				= UDP socket handle that can be used for network communications
//		char *broadcastAddress	= The broadcast address that should be used for the current LAN
//		char *broadcastPort		= The port that should be used for the broadcast
//
//	OUT parameters:
//		ServerStruct server[]	= An array that contains a struct for each server found.  The
//								  members of each struct contain the name, IP Address and port
//								  number of a remote server.
//		numServers				= Number of elements in the server[] array
//
//	Function return value	= Number of remote servers (size of server[] array)

	numServers = 0;

	// Send NIM_QUERY to broadcastAddress using broadcastPort
	
	/****
	Student should insert the necessary code here to send the NIM_QUERY string
	to the broadcastAddress.  The port field should be equal to broadcastPort.
	****/
	int lengthQueryMessage = (int)strlen(NIM_QUERY);
	char * messageToBeSent = new char[lengthQueryMessage+1];
	strcpy_s(messageToBeSent,lengthQueryMessage+1,NIM_QUERY);
	int totalSent = UDP_send(s,messageToBeSent,(int)strlen(messageToBeSent)+1,broadcastAddress,NIM_UDPPORT);
	delete [] messageToBeSent ;
	// Receive incoming UDP datagrams (with a maximum of 2 second wait before each recv() function call)
	// As you read datagrams, if they start with the prefix: NIM_NAME, parse out the server's name
	// and add the name, host address and port number to the server[] array.  Don't forget to increment numServers.
	// On the other hand, if the datagram doesn't start with the NIM_NAME prefix, ignore it!
	
	/****
	Student should insert code here that will accomplish the task outlined in the previous
	paragraph.  After this code section executes, the server[] array should be populated with
	names, addresses & port numbers of all servers.  The integer variable, numServers, should
	contain the number of servers found.
	****/
	
	int status = wait(s,SECs_TO_WAIT,MIL_TO_WAIT);
	
	while(numServers < MAX_SERVERS  && status > 0)
	{	
		ServerStruct serverStruct;
		char buf[MAX_RECV_BUF];
		char host[MAX_HOST_SIZE];
		char port[MAX_SERVICE_SIZE];
		
		int lengthOfName = strlen(NIM_NAME);
		int lengthOfMessageReceived = UDP_recv(s,buf,MAX_RECV_BUF,host,port);
		
//		if( _strnicmp(buf,NIM_NAME,lengthOfName) == 0)
//		{
//			strcpy_s(serverStruct.name, buf+lengthOfName);
		    strcpy_s(serverStruct.name, buf);
			strcpy_s(serverStruct.host, host);
			strcpy_s(serverStruct.port, port);
			
			server[numServers] = serverStruct;
			numServers++;
//		}else 
//		{
//			continueReadingDatagrams = false;
//		}

		status = wait(s,SECs_TO_WAIT,MIL_TO_WAIT);
	}
}
